<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>

						<el-col :span="8">
							<el-form-item label="线路编号" prop="code">
								<el-input v-model="tableData.param.code" placeholder="请输入线路编号" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="线路名称" prop="name">
								<el-input v-model="tableData.param.name" placeholder="请输入线路名称" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" @click="handleAdd" v-if="permissionStore.isPermissionsBtn('012006001')"><el-icon>
										<Plus />
									</el-icon>新增</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="线路编号" align="center" prop="code" min-width="100px" />
				<el-table-column label="线路名称" align="center" prop="name" min-width="100px"/>
				<el-table-column label="备注" align="center" prop="desc" min-width="200px"  show-overflow-tooltip/>
				
				<el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right" v-if="permissionStore.isPermissionsBtn(['012006002','012006003'])"
					>
					<template #default="scope">
						<el-button type="primary" link @click="handleEdit(scope.row)" v-if="permissionStore.isPermissionsBtn('012006002')"
							><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
						<el-button type="danger" link @click="handleDelete(scope.row)" v-if="permissionStore.isPermissionsBtn('012006003')"
							><el-icon>
								<Delete />
							</el-icon>删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
		<addView ref="addRef"  @getList="getList"></addView>
		<editView ref="editRef" @getList="getList"></editView>
	</div>
</template>
<script lang="ts">
import { outcallLineListApi,delOutcallLineApi } from '@/api/operationsManage/outboundLine';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission";
import addView from './components/add.vue'
import editView from './components/edit.vue'
export default defineComponent({
	name: 'outcallLineList',		//外呼线路管理
	components: { addView,editView },
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref();
		const editRef = ref();
		const companyAllotViewRef = ref();
		const permissionStore = usePermissionStore()
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				param: {
					pageNumber: 1,
					pageLength: 10,
					code: undefined,
					name: undefined,
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮登录 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			outcallLineListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};

		// 新增
		const handleAdd = () => {
			addRef.value.openDialog()
		}
		// 修改
		const handleEdit = (row) => {
			const rawRow = structuredClone(toRaw(row));
			editRef.value.openDialog(rawRow);
		}

		// 删除
		const handleDelete = (row: any) => {
			let msg = `此操作将永久删除数据：[${row.name}]，是否继续?`;
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delOutcallLineApi({ id:row.code }).then(() => {
						ElMessage.success('删除成功');
						getList();
					})
				})
				.catch(() => { });
		};
		
		return {
			proxy,
			loading,
			queryRef,
			addRef,
			editRef,
			companyAllotViewRef,
			permissionStore,
			resetQuery,
			getList,
			handleAdd,
			handleEdit,
			handleDelete,
			...toRefs(state),
		};
	},
});
</script>
